<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\controller\Fornt;
use app\common\model\Topup as TopupModel;
use app\common\model\TopupList;
use app\common\model\TopupLog;
use think\Request;
use think\Db;
/**
 * 充值控制器
 */
class Topup extends Fornt{

	public function index(){


    return $this->fetch();
	}

	public function add(){
		$card = input('post.card');

		//判断是否注册
    $wxuser_info = Db::name('wxUser')->where('id', USER_ID)->find();

		if(empty($wxuser_info['name']) || empty($wxuser_info['phone'])){
			return ['code'=>3, 'msg'=>'请先注册成为会员'];
		}

		if($card){
			$info = Db::name('topupList')->where('code', $card)->find();
			if($info['status'] == 1){
				$this->error('充值卡已经使用！');
			}
			if($info){
				//充值余额
				$return = Db::name('wxUser')->where('id', USER_ID)->setInc('balance', $info['sum']);
				if($return){
					$before_balance = $wxuser_info['balance'];


					Db::name('topupList')->where('code', $card)->update(['status'=>1, 'use_time'=>time(), 'uid'=>USER_ID]);


					//发送短信给用户
					$wxuser_info = Db::name('wxUser')->where('id', USER_ID)->find();
           sendcomSms('SMS_67595309', ['user'=>$wxuser_info['name'], 'time'=>date('Y-m-d H:i:s', time()), 'topup'=>(string)$info['sum'], 'balance'=>(string)$wxuser_info['balance'], 'integral'=>(string)$wxuser_info['integral']], (string)$wxuser_info['phone']);

					//发送短信给管理员
					sendcomSms('SMS_68070113', ['user'=>$wxuser_info['name'].'，微信名'.$wxuser_info['nickname'].'。', 'topup'=>(string)$info['sum'], 'balance'=>(string)$wxuser_info['balance'], 'integral'=>(string)$wxuser_info['integral']], '15885108322');
					sendcomSms('SMS_68070113', ['user'=>$wxuser_info['name'].'，微信名'.$wxuser_info['nickname'].'。', 'topup'=>(string)$info['sum'], 'balance'=>(string)$wxuser_info['balance'], 'integral'=>(string)$wxuser_info['integral']], '13638516023');


					update_session(USER_ID);


					//充值记录
          $dataTcord['user_id'] =  USER_ID;
          $dataTcord['sum'] =  $info['sum'];
          $dataTcord['status'] =  1;
          $dataTcord['create_time'] =  time();
          $dataTcord['topuplist_id'] =  $info['id'];
          $dataTcord['before_balance'] =  $before_balance;
          $dataTcord['after_balance'] =  $wxuser_info['balance'];
          $dataTcord['description'] =  '前台充值,充值码为'. $card;
          db('topupLog')->insert($dataTcord);


					$this->success('充值成功!');
				}

			}else{
				$this->error('充值卡号错误！');
			}
		}

	}

}
